<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="仓库ID" prop="warehouseId">
        <el-input
          v-model="queryParams.warehouseId"
          placeholder="请输入仓库ID"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="盘点人" prop="userId">
        <el-input
          v-model="queryParams.userId"
          placeholder="请输入盘点人"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="状态" prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="更新时间" style="width: 308px">
        <el-date-picker
          v-model="daterangeUpdateTime"
          value-format="YYYY-MM-DD"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['wms:stockCheck:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['wms:stockCheck:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['wms:stockCheck:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['wms:stockCheck:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="stockCheckList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="事务ID" align="center" prop="id" />
      <el-table-column label="仓库ID" align="center" prop="warehouseId" />
      <el-table-column label="盘点人" align="center" prop="userId" />
      <el-table-column label="状态" align="center" prop="state" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建者" align="center" prop="createBy" />
      <el-table-column label="入库时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新者" align="center" prop="updateBy" />
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['wms:stockCheck:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['wms:stockCheck:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓储盘点对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="stockCheckRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="仓库ID" prop="warehouseId">
          <el-input v-model="form.warehouseId" placeholder="请输入仓库ID" />
        </el-form-item>
        <el-form-item label="盘点人" prop="userId">
          <el-input v-model="form.userId" placeholder="请输入盘点人" />
        </el-form-item>
        <el-form-item label="状态" prop="state">
          <el-input v-model="form.state" placeholder="请输入状态" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-divider content-position="center">库存盘点信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="Plus" @click="handleAddWmsGoodsStockCheckLine">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="Delete" @click="handleDeleteWmsGoodsStockCheckLine">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="wmsGoodsStockCheckLineList" :row-class-name="rowWmsGoodsStockCheckLineIndex" @selection-change="handleWmsGoodsStockCheckLineSelectionChange" ref="wmsGoodsStockCheckLine">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="物品编码" prop="goodsCode" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.goodsCode" placeholder="请输入物品编码" />
            </template>
          </el-table-column>
          <el-table-column label="单位" prop="measureUnit" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.measureUnit" placeholder="请输入单位" />
            </template>
          </el-table-column>
          <el-table-column label="入库批次号" prop="batchCode" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.batchCode" placeholder="请输入入库批次号" />
            </template>
          </el-table-column>
          <el-table-column label="仓库ID" prop="warehouseId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.warehouseId" placeholder="请输入仓库ID" />
            </template>
          </el-table-column>
          <el-table-column label="库位ID" prop="areaId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.areaId" placeholder="请输入库位ID" />
            </template>
          </el-table-column>
          <el-table-column label="供应商ID" prop="supplierId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.supplierId" placeholder="请输入供应商ID" />
            </template>
          </el-table-column>
          <el-table-column label="生产厂商id" prop="manufacturerId" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.manufacturerId" placeholder="请输入生产厂商id" />
            </template>
          </el-table-column>
          <el-table-column label="在库数量" prop="quantity" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.quantity" placeholder="请输入在库数量" />
            </template>
          </el-table-column>
          <el-table-column label="库存有效期" prop="expireDate" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.expireDate"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择库存有效期">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="创建者" prop="createBy" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.createBy" placeholder="请输入创建者" />
            </template>
          </el-table-column>
          <el-table-column label="入库时间" prop="createTime" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.createTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择入库时间">
              </el-date-picker>
            </template>
          </el-table-column>
          <el-table-column label="更新者" prop="updateBy" width="150">
            <template #default="scope">
              <el-input v-model="scope.row.updateBy" placeholder="请输入更新者" />
            </template>
          </el-table-column>
          <el-table-column label="更新时间" prop="updateTime" width="240">
            <template #default="scope">
              <el-date-picker clearable
                v-model="scope.row.updateTime"
                type="date"
                value-format="YYYY-MM-DD"
                placeholder="请选择更新时间">
              </el-date-picker>
            </template>
          </el-table-column>
        </el-table>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="StockCheck">
import { listStockCheck, getStockCheck, delStockCheck, addStockCheck, updateStockCheck } from "@/api/wms/stockCheck";

const { proxy } = getCurrentInstance();

const stockCheckList = ref([]);
const wmsGoodsStockCheckLineList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const checkedWmsGoodsStockCheckLine = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");
const daterangeUpdateTime = ref([]);

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    warehouseId: null,
    userId: null,
    state: null,
    updateTime: null
  },
  rules: {
    warehouseId: [
      { required: true, message: "仓库ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询仓储盘点列表 */
function getList() {
  loading.value = true;
  queryParams.value.params = {};
  if (null != daterangeUpdateTime && '' != daterangeUpdateTime) {
    queryParams.value.params["beginUpdateTime"] = daterangeUpdateTime.value[0];
    queryParams.value.params["endUpdateTime"] = daterangeUpdateTime.value[1];
  }
  listStockCheck(queryParams.value).then(response => {
    stockCheckList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    warehouseId: null,
    userId: null,
    state: null,
    remark: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  };
  wmsGoodsStockCheckLineList.value = [];
  proxy.resetForm("stockCheckRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  daterangeUpdateTime.value = [];
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加仓储盘点";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getStockCheck(_id).then(response => {
    form.value = response.data;
    wmsGoodsStockCheckLineList.value = response.data.wmsGoodsStockCheckLineList;
    open.value = true;
    title.value = "修改仓储盘点";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["stockCheckRef"].validate(valid => {
    if (valid) {
      form.value.wmsGoodsStockCheckLineList = wmsGoodsStockCheckLineList.value;
      if (form.value.id != null) {
        updateStockCheck(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addStockCheck(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除仓储盘点编号为"' + _ids + '"的数据项？').then(function() {
    return delStockCheck(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 库存盘点序号 */
function rowWmsGoodsStockCheckLineIndex({ row, rowIndex }) {
  row.index = rowIndex + 1;
}

/** 库存盘点添加按钮操作 */
function handleAddWmsGoodsStockCheckLine() {
  let obj = {};
  obj.goodsCode = "";
  obj.measureUnit = "";
  obj.batchCode = "";
  obj.warehouseId = "";
  obj.areaId = "";
  obj.supplierId = "";
  obj.manufacturerId = "";
  obj.quantity = "";
  obj.expireDate = "";
  obj.remark = "";
  obj.createBy = "";
  obj.createTime = "";
  obj.updateBy = "";
  obj.updateTime = "";
  wmsGoodsStockCheckLineList.value.push(obj);
}

/** 库存盘点删除按钮操作 */
function handleDeleteWmsGoodsStockCheckLine() {
  if (checkedWmsGoodsStockCheckLine.value.length == 0) {
    proxy.$modal.msgError("请先选择要删除的库存盘点数据");
  } else {
    const wmsGoodsStockCheckLines = wmsGoodsStockCheckLineList.value;
    const checkedWmsGoodsStockCheckLines = checkedWmsGoodsStockCheckLine.value;
    wmsGoodsStockCheckLineList.value = wmsGoodsStockCheckLines.filter(function(item) {
      return checkedWmsGoodsStockCheckLines.indexOf(item.index) == -1
    });
  }
}

/** 复选框选中数据 */
function handleWmsGoodsStockCheckLineSelectionChange(selection) {
  checkedWmsGoodsStockCheckLine.value = selection.map(item => item.index)
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('wms/stockCheck/export', {
    ...queryParams.value
  }, `stockCheck_${new Date().getTime()}.xlsx`)
}

getList();
</script>
